print("Test master prototype loading...")
print("Trying to inherit...")
inherit("AbstractObject")
print("Done inheriting...")

-- random comment

function setuph3d(self)
  print("Setting up h3d")
  local h3d = TopObjects.h3d
  self.sw = 800
  self.sh = 600
  h3d:InitGraphics(self.sw, self.sh, false)
  print("Adding resources!")
  self.pipeRes = h3d:AddResource(h3d.H3DResTypes.Pipeline, "pipelines/forward.pipeline.xml", 0)
  --self.modelRes = h3d:AddResource(h3d.H3DResTypes.SceneGraph, "bla.bla.xml", 0)
  self.platRes = h3d:AddResource(h3d.H3DResTypes.SceneGraph, "models/platform/platform.scene.xml", 0)
  self.skyboxRes = h3d:AddResource(h3d.H3DResTypes.SceneGraph, "models/skybox/skybox.scene.xml", 0)
  self.fontRes = h3d:AddResource(h3d.H3DResTypes.Material, "overlays/font.material.xml", 0)
  self.panelRes = h3d:AddResource(h3d.H3DResTypes.Material, "overlays/panel.material.xml", 0)
  print("Loading resources!")
  h3d:LoadResourcesFromDisk()
  print("Adding lights and stuff!")
  --self.model = h3d:AddNodes(h3d.RootNode, self.modelRes)
  self.skybox = h3d:AddNodes(h3d.RootNode, self.skyboxRes)
  self.platform = h3d:AddNodes(h3d.RootNode, self.platRes)
  print("Added model! Adding light!")
  self.light = h3d:AddLightNode(h3d.RootNode, "Light1", h3d.NullRes, "LIGHTING", "SHADOWMAP")
  print("Setting transforms and whatnot!")
  h3d:SetNodeTransform(self.skybox, 0, 0, 0, 0, 0, 0, 210, 210, 210)
  h3d:SetNodeTransform(self.platform, 0, 0, 0, 0, 0, 0, 0.23, 0.23, 0.23)
  h3d:SetNodeTransform(self.light, 0, 20, 50, -30, 0, 0, 1, 1, 1)
  print("C")
  h3d:SetNodeParamF(self.light, h3d.H3DLight.RadiusF, 0, 300.0)
  print("D")
  h3d:SetNodeParamF(self.light, h3d.H3DLight.FovF, 0, 90)
  print("E")
  h3d:SetNodeParamI(self.light, h3d.H3DLight.ShadowMapCountI, 3)
  print("F")
  h3d:SetNodeParamF(self.light, h3d.H3DLight.ShadowSplitLambdaF, 0, 0.9)
  print("G")
  h3d:SetNodeParamF(self.light, h3d.H3DLight.ShadowMapBiasF, 0, 0.0015)
  h3d:SetNodeParamF(self.light, h3d.H3DLight.ColorF3, 0, 0.9)
  h3d:SetNodeParamF(self.light, h3d.H3DLight.ColorF3, 1, 0.9)
  h3d:SetNodeParamF(self.light, h3d.H3DLight.ColorF3, 2, 0.75)
  print("Camera!")

  self.cam = h3d:AddCameraNode(h3d.RootNode, "Camera", self.pipeRes)

  h3d:EnableAutoUpdate(self.cam)
  h3d:DumpMessages()
end

function dispatch_message(self, msg)
  print("message!")
  for k,v in _G.pairs(msg) do
    print(k .. ": " .. (v or "nil"))
  end
end

function mouseMoveEvent(self, msg)
  print("Mouse move event!")
  print("x: " .. msg.x .. ", y: " .. msg.y)
  print("dx: " .. msg.dx .. ", dy: " .. msg.dy)

  self.ry = self.ry - msg.dx / 100 * 60
  self.rx = self.rx - msg.dy / 100 * 60
  if self.rx > 90 then self.rx = 90 end
  if self.rx < -90 then self.rx = -90 end
  
  if self.gui ~= nil then self.gui:mouseMove(msg) end
end

function keyEvent(self, msg)
  self:dispatch_message(msg)
  self.keys[msg.keyName] = (msg.keyDown == 1)
  if msg.keyDown == 1 and msg.keyName == "F5" then
    TopObjects.Revel:StartEncoding(self.sw, self.sh, 60.0, 30, "video_out.avi")
    self.encoding = true
  end
  if msg.keyDown == 1 and msg.keyName == "F8" then
    TopObjects.Revel:FinalizeEncoding()
    self.encoding = false
  end
  if msg.keyDown == 1 and msg.keyName == "F2" then
    self:grabFrame()
  end
  if msg.keyDown == 1 and msg.keyName == "F1" then
	self.gui:getBase().enabled = not self.gui:getBase().enabled
  end
end

function mouseButtonEvent(self,msg)
  self:dispatch_message(msg)
  if self.gui ~= nil then self.gui:mouseButton(msg) end
end

function grabFrame(self)
  self.frame = TopObjects.h3d:GetScreenshot(self.frame)
  if self.frame then
    print("Frame size: " .. self.frame:SizeX() .. " " .. self.frame:SizeY())
    local mx = self.frame:SizeX() / 2
    local my = self.frame:SizeY() / 2
    local r = self.frame:Get(mx, my, 0)
    local g = self.frame:Get(mx, my, 1)
    local b = self.frame:Get(mx, my, 2)
    print("Center rgb: " .. r .. " " .. g .. " " .. b)
  end
end

function init(self)
  print("Test master init!")
  _G.print("Does this print work?")

  for k,v in _G.pairs(_G.Core) do
    print("Core." .. k)
  end

  -- print what our sysargs are...
  local sargs = _G.Core.GetSysArgs()
  for k,v in _G.pairs(sargs) do
    local tempstr = k .. "{"
    for i,v2 in _G.ipairs(v) do
      tempstr = tempstr .. v2 .. ","
    end
    tempstr = tempstr .. "}"
    print(tempstr)
  end

  print("Trying to make a teapot...")
  local h3d_dummy = _G.Core.CreateTLO("Horde3d", "h3d")
  local tempo = _G.Core.CreateTLO("game.test2", "Teapot")
  tempo:init()
  tempo:teapot("_foo_")
  TopObjects.Teapot:teapot("_bar_")
  TopObjects.Teapot:barpot("_notbar_")

  local revel = _G.Core.CreateTLO("RevelEncoder", "Revel")

  self:setuph3d()

  local dp = _G.Core.CreateTLO("Dispatch", "disp")
  --dp:AddHandler("event:mouseMove", self)
  --dp:AddHandler("event:keyEvent", self)
  --dp:AddHandler("event:mouseButton", self)
  dp:AddNamedHandler("event:mouseMove", "mouseMoveEvent", self)
  dp:AddNamedHandler("event:keyEvent", "keyEvent", self)
  dp:AddNamedHandler("event:mouseButton", "mouseButtonEvent", self)
  
  -- go go gui stuff...
  self.guiroot = Core.CreateTLO("gui.SimpleGUIRoot", "guiroot")
  self.guidraw = Core.CreateTLO("gui.H3dDrawer", "guidraw")
  self.guidraw:init()
  self.guiroot:init(self.guidraw)
  self.guiroot.baseMaterial = self.guidraw:makeGuiMat(self.panelRes, "basematerial")
  self.guiroot:setPixelDimensions(self.sw, self.sh)
  print("bjfjfj: " .. tostring(self.guiroot.baseMaterial))
  local basefont = self.guidraw:makeFontMat(self.fontRes, "basefont", {1,1,1,1})
  --local basefont = self.guidraw:makeFontMat(self.panelRes, "basefont")
  local gui = self.guiroot
  gui:setBase(gui:newComponent("gui.Panel"))
  gui:getBase():setSize((800/600), 1.0)
  gui:getBase().material = nil -- don't want to draw a background for the root panel
  local nlabel = gui:newComponent("gui.FWLabel")
  nlabel:setFont(basefont, 0.05, 0.05)
  nlabel:setPosition(0.05, 0.15)
  nlabel:setSize(0.6, 0.05)
  nlabel.col = {0,0,0,1}
  nlabel:setText("TEXT")
  nlabel.spacing = -0.5
  local nel = gui:newComponent("gui.Element")
  nel:setSize(0.7, 0.5)
  nel:setPosition(0.3, 0.3)
  local nwindow = gui:newComponent("gui.Window")
  nwindow:setFont(basefont, 1.0, 1.0)
  nwindow:setSize(0.7, 0.5)
  nwindow:setPosition(0.3, 0.3)
  nwindow.borderwidth = 0.005
  nwindow:updateBorders()
  --gui:getBase():addChild(nel)
  --gui:getBase():addChild(nlabel)
  gui:getBase():addChild(nwindow)
  nwindow.panel:addChild(nlabel)
  self.label = nlabel
  gui:printoutAllComps()
  self.gui = gui
  self.gui:getBase().enabled = false

  self.timeleft = 10
  self.x, self.y, self.z = 0, 0, 0
  self.rx, self.ry, self.rz = 0, 0, 0
  self.keys = {}
end

function degToRad(val)
  return (val / 180.0) * 3.1415926
end

function doCameraMovement(self, dt)
  local curvel = dt * 10
  local math = _G.math

  if self.keys["w"] then
    self.x = self.x - (math.sin(degToRad(self.ry)) * math.cos(-1 * degToRad(self.rx))) * curvel
    self.y = self.y - math.sin(-1 * degToRad(self.rx)) * curvel
    self.z = self.z - math.cos(degToRad(self.ry)) * math.cos(-1 * degToRad(self.rx)) * curvel
  end
  
  if self.keys["a"] then
    self.x = self.x - math.cos(degToRad(self.ry)) * curvel
    self.z = self.z + math.sin(degToRad(self.ry)) * curvel
  end

  if self.keys["s"] then
    self.x = self.x + (math.sin(degToRad(self.ry)) * math.cos(-1 * degToRad(self.rx))) * curvel
    self.y = self.y + math.sin(-1 * degToRad(self.rx)) * curvel
    self.z = self.z + math.cos(degToRad(self.ry)) * math.cos(-1 * degToRad(self.rx)) * curvel
  end

  if self.keys["d"] then
    self.x = self.x + math.cos(degToRad(self.ry)) * curvel
    self.z = self.z - math.sin(degToRad(self.ry)) * curvel
  end

  if self.keys["SPACE"] then
    self.y = self.y + curvel
  end

  if self.keys["LCTRL"] then
    self.y = self.y - curvel
  end

  TopObjects.h3d:SetNodeTransform(self.cam, self.x, self.y, self.z, self.rx, self.ry, self.rz, 1, 1, 1)

  if self.encoding then
    self.frame = TopObjects.h3d:GetScreenshot(self.frame)
    if self.frame ~= nil then
      TopObjects.Revel:EncodeFrame(self.frame)
    end
  end
end

function update(self, dt)
  self:doCameraMovement(dt)
  --print("Test master update: " .. self.timeleft)
  self.timeleft = self.timeleft - 1
  local ttext = "nil"
  if self.gui.hover ~= nil then ttext = self.gui.hover:getName() end
  self.label:setText(ttext)
  --if (self.timeleft <= 0) then
  --  _G.Core.Quit()
  --end
end